<?php
namespace Foxframe;
require_once 'library/smarty/Smarty.class.php';

class View extends \Smarty
{
	public $extension	= '.html';
	public $showLayout	= true;
	public $smarty;
	public $html;
	public $theme		= 'responsive';

	public function __construct()
	{
		$this->setTemplateDir(APP_PATH.Uri::getInstance()->get('application').'/View/'.Uri::getInstance()->get('controller'));
		$this->setCompileDir('tmp/');
		$this->setCaching(false);
		$this->force_compile	= true;
		$this->error_reporting	= E_ALL & ~E_NOTICE;
		$this->assign('PUBLIC_PATH',PUBLIC_PATH);
		$this->assign('BASE_PATH', BASE_PATH);
		$this->assign('URL_PUBLIC_PATH',URL_PUBLIC_PATH);
		$this->assign('URL_BASE_PATH',URL_BASE_PATH);
		$this->assign('SESSION_LOGIN',SESSION_LOGIN);
		$this->assign('Uri', Uri::getInstance());
		$this->assign('Action', new \Helper\Action\Action());
		$this->smarty			= $this;

		return $this;
	}

	public function minify($html)
	{
		$html = preg_replace('(\r|\n|\t)', '', $html);

		return $html;
	}

	public function setTheme($theme)
	{
		$this->theme = $theme;
	}

	public function getTheme()
	{
		return $this->theme;
	}

	public function getHtml()
	{
		return $this->html;
	}

	public function setHtml($html)
	{
		$this->html = $html;
	}

	public function render($echo = true)
	{
		$uri		= \Foxframe\Uri::getInstance();
		$content	= $this->fetch($uri->get('method').$this->extension);
		if ($this->showLayout)
		{
			$menu = new \Foxframe\Menu(APP_PATH.$uri->get('application').'/Config/menu.yml');
			$this->assign('menuSistema', $menu->html);

			$this->assign('content', $content);
			$content	= $this->fetch("public/theme/{$this->getTheme()}/layout.html");
		}

		$this->setHtml($content);

		// 	$content	= $this->minify($content);
		if ($echo)
			echo $this->getHtml();
		else
			return $this->getHtml();
	}

	public function renderTheme($content)
	{
		$uri	= \Foxframe\Uri::getInstance();
		$menu	= new \Foxframe\Menu(APP_PATH.$uri->get('application').'/Config/menu.yml');
		$this->assign('menuSistema', $menu->html);

		$this->assign('content', $content);
		$content	= $this->fetch("public/theme/{$this->getTheme()}/layout.html");
		return $content;
	}
}